from manimlib.imports import *
import random

class Scene1(Scene):
    def construct(self):
        circ = Circle(radius=0.1, fill_color=RED,fill_opacity=1.0, stroke_opacity=0.0).move_to(3.5 * UP)
        self.play(ShowCreation(circ), run_time=0.5)
        k = 0
        step = 7/19
        for i in range(1, 17):
            y = step * UP * i
            for j in range(i-k):
                x = step * RIGHT * j
                square1 = Square(side_length=0.2, fill_color=GREEN,
                          fill_opacity=1.0, stroke_opacity=0.0).move_to(x-y+3.5*UP)
                square2 = Square(side_length=0.2,fill_color=GREEN,
                          fill_opacity=1.0, stroke_opacity=0.0).move_to(-x - y + 3.5 * UP)
                self.play(ShowCreation(square1), ShowCreation(square2), run_time=0.5)
            if i % 4 == 0:
                x = step * RIGHT * (j+1)
                circ1 = Circle(radius=0.1, fill_color=RED,fill_opacity=1.0, stroke_opacity=0.0).move_to(x - y + 3.5 * UP)
                circ2 = Circle(radius=0.1, fill_color=RED,fill_opacity=1.0, stroke_opacity=0.0).move_to(-x - y + 3.5 * UP)
                self.play(ShowCreation(circ1), ShowCreation(circ2), run_time=0.5)
                k = k + 3
            if i % 4 == 3:
                x = step * RIGHT * (j+1)
                circ1 = Circle(radius=0.1, fill_color=YELLOW,fill_opacity=1.0, stroke_opacity=0.0).move_to(x - y + 3.5 * UP)
                circ2 = Circle(radius=0.1, fill_color=YELLOW,fill_opacity=1.0, stroke_opacity=0.0).move_to(-x - y + 3.5 * UP)
                self.play(ShowCreation(circ1), ShowCreation(circ2), run_time=0.5)

        for i in range(17, 21):
            y = step * UP * i
            for j in range(3):
                x = step * RIGHT * j
                square1 = Square(side_length=0.2, fill_color=GREY_BROWN,
                                 fill_opacity=1.0, stroke_opacity=0.0).move_to(x - y + 3.5 * UP)
                square2 = Square(side_length=0.2, fill_color=GREY_BROWN,
                                 fill_opacity=1.0, stroke_opacity=0.0).move_to(-x - y + 3.5 * UP)
                self.play(ShowCreation(square1), ShowCreation(square2), run_time=0.5)

        self.snowArr = []
        for i in range(20):
            line = Line(start=0.25*LEFT, end=0.25*RIGHT)
            line1 = Line(start=0.25*LEFT, end=0.25*RIGHT).rotate(60*DEGREES)
            line2 = Line(start=0.25 * LEFT, end=0.25 * RIGHT).rotate(120 * DEGREES)
            snow = VGroup(line, line1, line2)
            x = (round(random.random(), 3) - 0.5) * 14 * RIGHT
            y = (round(random.random(), 3) - 0.5) * UP + 3 * UP
            snow.v_rate = 0.1 * random.random() * DOWN
            snow.r_rate = 0.1 * random.random()
            self.snowArr.append(snow.move_to(x+y))
            self.play(ShowCreation(snow), run_time=0.2)

        for i in range(100):
            for j in range(len(self.snowArr)):
                self.snowArr[j].shift(self.snowArr[j].v_rate).rotate(self.snowArr[j].r_rate)
            self.wait(0.1)

        self.clear()

        ctx = TextMobject('祝大家圣诞快乐!').set_color_by_gradient(GREEN, RED, BLUE)
        self.play(Write(ctx))


        self.wait(2)
